package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.dto.ApiResponseForList;
import com.fuhe.chen.vendingmachine.dto.CommodityInMachine;
import com.fuhe.chen.vendingmachine.dto.cond.CommodityCond;
import com.fuhe.chen.vendingmachine.pojo.Commodity;
import com.fuhe.chen.vendingmachine.pojo.CommodityPicture;
import com.fuhe.chen.vendingmachine.pojo.CommoditySpecification;
import com.fuhe.chen.vendingmachine.service.ICategoryService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品管理首页
 * 新增商品/修改商品信息/删除商品信息
 */
@Controller
@Api(tags = "商品信息管理")
@RequestMapping("/admin/commodity")
public class CommodityController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommodityController.class);

    @Autowired
    ICommodityService commodityService;

    @Autowired
    ICategoryService categoryService;


    /**
     * 新增单个商品信息
     */
    @ApiOperation(value = "新增商品信息",notes = "新增商品为下架状态,需要手动上架")
    @PostMapping("/addComm")
    @ResponseBody
    @AddLog(operation = "新增商品信息")
    public String addComm(@RequestParam("commodityName") String commodityName,
                          @RequestParam(value = "comment",required = false) String comment,
                          @RequestParam("categoryId") String categoryId) {


        Commodity commodity = new Commodity();
        commodity.setCommodityName(commodityName);
        commodity.setCategoryId(Integer.parseInt(categoryId));
        commodity.setCommodityStatus(0);
        commodity.setComment(comment);

        commodityService.addCommodity(commodity);

        return String.valueOf(commodity.getId());
    }

    /**
     * 修改单个商品信息
     */
    @ApiOperation(value = "修改商品信息")
    @PostMapping("/updateComm")
    @ResponseBody
    @AddLog(operation = "修改商品信息")
    public ApiResponseForList<String> updateComm(Integer commodityId, String commodityName, Integer categoryId) {

        Commodity commodity = new Commodity();
        commodity.setId(commodityId);
        commodity.setCommodityName(commodityName);
        commodity.setCategoryId(categoryId);
        commodityService.updateCommodity(commodity);

        ApiResponseForList<String> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("修改成功");

        return response;
    }


    @ApiOperation(value = "商品下架")
    @PostMapping("/offTheShelf")
    @ResponseBody
    @AddLog(operation = "商品下架")
    public ApiResponseForList<String> offTheShelf(Integer commodityId) {

        Commodity commodity = new Commodity();
        commodity.setId(commodityId);
        commodity.setCommodityStatus(0);

        commodityService.updateCommodity(commodity);

        ApiResponseForList<String> response = new ApiResponseForList<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("商品下架成功");

        return response;
    }


    @ApiOperation(value = "商品上架")
    @PostMapping("/onTheMarket")
    @ResponseBody
    @AddLog(operation = "商品上架")
    public ApiResponseForList<String> onTheMarket(Integer commodityId) {
        ApiResponseForList<String> response = new ApiResponseForList<>();

        Commodity commodity = commodityService.queryOne(commodityId);
        List<CommodityPicture> picture = commodity.getPicture();
        List<CommoditySpecification> specifications = commodity.getSpecifications();

        if(picture!=null&&picture.size()>0&&specifications!=null&&specifications.size()>0){
            commodity.setCommodityStatus(1);
            commodityService.updateCommodity(commodity);
            response.setSuccess(true);
            response.setCode(10000);
            response.setMessage("商品上架成功");
        }else{
            response.setSuccess(false);
            response.setCode(12222);
            response.setMessage("上架失败,商品至少要有一个图片和规格才能进行上架操作");
        }
        return response;
    }

    /**
     * 删除指定商品信息
     *
     * @param commodityId
     * @return
     */
    @ApiOperation(value = "删除商品信息")
    @PostMapping("/deleteComm")
    @ResponseBody
    @AddLog(operation = "删除商品信息")
    public String deleteComm(@RequestParam("commodityId") Integer commodityId) {

        commodityService.deleteCommodity(commodityId);

        return "删除成功";
    }

    @ApiOperation(value = "查询商品信息")
    @ResponseBody
    @GetMapping("/queryCommodity")
    public ApiResponse<PageInfo<Commodity>> queryCommodity(
            String commodity_name,
            Integer category,
            Integer commodity_status,
            Integer pageNum,
            Integer pageSize) {
        CommodityCond cond = new CommodityCond();
        cond.setCategory(category);
        cond.setCommodity_name(commodity_name);
        cond.setCommodity_status(commodity_status);

        PageInfo<Commodity> commodities = commodityService.queryCommodityByCondition(cond, pageNum, pageSize);

        ApiResponse<PageInfo<Commodity>> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("调用成功");
        response.setData(commodities);
        return response;
    }


    @ApiOperation(value = "根据id获取商品详情")
    @GetMapping({"/getDetail/{id}"})
    @ResponseBody
    public ApiResponse<Commodity> getCommodityDetail(@PathVariable Integer id) {
        ApiResponse<Commodity> response = new ApiResponse<>();
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("调用成功");
        Commodity commodity = commodityService.queryOne(id);
        response.setData(commodity);
        return response;
    }

}
